package com.example.fw;

import com.example.tests.GroupData;
import com.example.utils.SortedListOf;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: pshevchenko
 * Date: 06.03.13
 * Time: 18:55
 * To change this template use File | Settings | File Templates.
 */
public class GroupHelper extends WebDriverHelperBase {

    private GroupHelper selectGroupByIndex(int groupIndex) {
        clickElement(By.xpath("//input[@name=\"selected[]\"][" + (groupIndex + 1) + "]"));
        return this;
    }

/*    private void rebuildCache() {
        cachedGroups = new SortedListOf<GroupData>();
        List<WebElement> checkBoxList = driver.findElements(By.name("selected[]"));
        for (WebElement checkBox : checkBoxList){
            String title = checkBox.getAttribute("title");
            String groupName = title.substring("Select (".length(), title.length() - ")".length());
            cachedGroups.add(new GroupData()
                    .setName(groupName));
        }
    }

    private void clearCache() {
        cachedGroups = null;
        manager.getAddressHelper().reportGroupModification();
    }*/

//----------------------------------------------------------------------------------------------------------------------

    public GroupHelper(ApplicationManager manager) {
        super(manager);
    }

    public GroupHelper createGroup(GroupData groupData) {
        manager.navigateTo().groupsList();
        gotoGroupCreation();
        fillGroupForm(groupData);
        submitGroupData();
        returnToGroupsList();
        // update model
        manager.getModel()
                .addGroup(groupData);
        return this;
    }

    public GroupHelper deleteGroup(int groupIndex) {
        manager.navigateTo().groupsList();
        selectGroupByIndex(groupIndex);
        submitGroupRemoval();
        returnToGroupsList();
        // update model
        manager.getModel()
                .removeGroup(groupIndex);
        return this;
    }

    public GroupData modifyGroup(GroupData groupData, int groupNumber) {
        manager.navigateTo().groupsList();
        gotoGroupModification(groupNumber);
        GroupData expectedGroupData = groupData.storeUnChangedFields(readGroupDataFromCard());
        fillGroupForm(groupData);
        submitGroupModification();
        returnToGroupsList();
        manager.getModel()
                .removeGroup(groupNumber)
                .addGroup(expectedGroupData);

        // update model

        return expectedGroupData;
    }

//----------------------------------------------------------------------------------------------------------------------

    /*public SortedListOf<GroupData> getGroupList() {
        if (cachedGroups == null)
            // update model
            rebuildCache();
        return cachedGroups;
    } */

    public GroupHelper gotoGroupCreation() {
        clickElement(By.name("new"));
        return this;
    }

    public GroupHelper submitGroupData() {
        clickElement(By.name("submit"));
        return this;
    }

    public GroupHelper fillGroupForm(GroupData groupData) {
        fillTextField(By.name("group_name"), groupData.getGroupName());
        fillTextField(By.name("group_header"), groupData.getGroupHeader());
        fillTextField(By.name("group_footer"), groupData.getGroupFooter());
        return this;
    }

    public GroupHelper returnToGroupsList() {
        clickElement(By.linkText("group page"));
        return this;
    }

    public String readGroupNameFromList(int groupIndex) {
        String result;
        result = driver.findElement(By.xpath("//input[@name=\"selected[]\"][" + (groupIndex + 1) + "]")).getAttribute("title");
        result = result.substring("Select (".length(), result.length() - ")".length());
        return result;

    }

    public GroupHelper gotoGroupModification(int groupIndex) {
        selectGroupByIndex(groupIndex);
        clickElement(By.name("edit"));
        return this;
    }

    public GroupHelper submitGroupModification() {
        clickElement(By.name("update"));
        return this;
    }

    public GroupHelper deleteSeveralGroups(int[] groupIndexArray) {
        for (int i : groupIndexArray){
            selectGroupByIndex(i);
            return this;
        }
        submitGroupRemoval();

        return this;
    }

    public GroupHelper submitGroupRemoval() {
        clickElement(By.name("delete"));
        return this;
    }

    public GroupData readGroupDataFromCard(){
        GroupData result = new GroupData();

        result.setGroupHeader(readGroupHeader());
        result.setGroupFooter(readGroupFooter());
        result.setGroupName(readGroupName());

        return result;
    }

    public String readGroupName(){
        return getFieldValue(By.name("group_name"));
    }

    public String readGroupHeader(){
        return getFieldValue(By.name("group_header"));
    }

    public String readGroupFooter(){
        return getFieldValue(By.name("group_footer"));
    }

    public SortedListOf<GroupData> getUiGroupList() {
        SortedListOf<GroupData> uiGroupList = new SortedListOf<GroupData>();
        List<WebElement> checkBoxList = driver.findElements(By.name("selected[]"));
        for (WebElement checkBox : checkBoxList){
            String title = checkBox.getAttribute("title");
            String groupName = title.substring("Select (".length(), title.length() - ")".length());
            uiGroupList.add(new GroupData()
                    .setName(groupName));
        }
        return uiGroupList;
    }
}
